﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PL_TileEditor
{
    public partial class PortalViewer : Form
    {
        public DialogResult DiagResult = DialogResult.None;
        public string DestMap;
        public Point DestPoint;
        public int TriggerButton = 0;

        public PortalViewer()
        {
            InitializeComponent();
            mapViewer1.Tiles = tileViewer1;
            mapViewer1.CurrentTool = MapViewer.Tool.None;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileStream tempFile = new FileStream(openFileDialog1.FileName, FileMode.Open);
                BinaryReader br = new BinaryReader(tempFile);
                int Version = br.ReadInt32();
                int NameLength = br.Read();
                char[] TileImage = new char[NameLength];
                br.Read(TileImage, 0, NameLength);
                tileViewer1.ChangeImage("../tiles/" + new string(TileImage));
                DestMap = Path.GetFileName(openFileDialog1.FileName);
                Size TileImageSize = new Size(br.ReadInt32(), br.ReadInt32());
                Size TileSize = new Size(br.ReadInt32(), br.ReadInt32());
                tileViewer1.ChangeTileSize(TileSize.Width, TileSize.Height);
                for (int i = 0; i < TileImageSize.Width * TileImageSize.Height; i++)
                {
                    tileViewer1.TileProperties[i].IsSolid = br.ReadBoolean();
                }
                tileViewer1.SelectedTile = 0;
                Size MapSize = new Size(br.ReadInt32(), br.ReadInt32());
                mapViewer1.ChangeSize(MapSize);
                for (int z = 0; z < 3; z++)
                {
                    for (int x = 0; x < MapSize.Width; x++)
                    {
                        for (int y = 0; y < MapSize.Height; y++)
                        {
                            mapViewer1.TileMap[z, x, y] = 9999;
                        }
                    }
                }
                int NumTiles = br.ReadInt32();
                for (int i = 0; i < NumTiles; i++)
                {
                    int x = br.ReadInt32();
                    int y = br.ReadInt32();
                    int z = br.ReadInt32();
                    mapViewer1.TileMap[z, x, y] = br.ReadInt32();
                }


                tempFile.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DiagResult = DialogResult.OK;
            Close();
        }

        private void mapViewer1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                DestPoint = new Point(mapViewer1.SelectedTile.X * tileViewer1.TileSize.Width, mapViewer1.SelectedTile.Y * tileViewer1.TileSize.Height);
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            TriggerButton = 0;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            TriggerButton = 1;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            TriggerButton = 2;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            TriggerButton = 3;
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            TriggerButton = 4;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DiagResult = DialogResult.Cancel;
            Close();
        }
    }
}
